home *** CD-ROM | disk | FTP | other *** search
- Path: ix.netcom.com!netnews
- From: dannyyoo@ix.netcom.com (Danny Yoo)
- Newsgroups: comp.lang.c++
- Subject: Re: Arrays-What are they good for?
- Date: Mon, 01 Jan 1996 06:00:52 GMT
- Organization: Netcom
- Message-ID: <30e77573.6571977@nntp.ix.netcom.com>
- References: <4c6fc5$jv3$1@mhadf.production.compuserve.com>
- NNTP-Posting-Host: ix-scr-ca1-16.ix.netcom.com
- X-NETCOM-Date: Sun Dec 31 10:08:56 PM PST 1995
- X-Newsreader: Forte Agent .99c/16.141
-
- Merlin Chowkwanyun <71702.1123@CompuServe.COM> wrote:
-
- >
- >Hello all....
- >
- >What are arrays? After reading about them countless times I cannot understand
- >their purpose nor how to use them. Can someone give me an example showing how
- >they can be used? Thank you..
-
- Arrays are variables that allow you to store lists or elements
- and other useful stuff. In math, arrays would be analogous to sets, I
- believe. You can use arrays to store a list of integers, chars, or
- any data type. The statement:
-
- int myintegers[10];
-
- creates a set of 10 integers, all under the name "myintegers".
- You can access any one of them by using its subscript within the
- brackets. For example, you could access the first element of
- myintegers like
-
- myintegers[0] = 1; Yes, it starts at zero. To access the
- tenth variable within myintegers, you'd use myintegers[9].
-
- You can use arrays in loops, to record a bunch of information.
- An illustration would be to record 10 numbers from the user and print
- them back out again. (Ok, it doesn't sound like a practical
- application, but it's simple.) Conventionally, you'd do something
- like:
- int a, b, c, d, e, f... etc... but this takes too long.
- (imagine if you have to work with 100 numbers, or names!) Instead,
- you can do this:
-
- int numbers[10], counter; // I'm using counter as my increment
- for (counter=0;counter<10;counter++)
- cin >> numbers[counter];
- for (counter=0;counter<10;counter++)
- cout << numbers[counter];
-
- Hope this helps!
-